package com.fc.config.shiro;


import com.fc.entity.SysUser;
import com.fc.service.SysUserService;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;
import org.springframework.beans.factory.annotation.Autowired;

public class ShiroRelam extends AuthorizingRealm {
    @Autowired
    private SysUserService userService;

    //前端用户
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        return null;
    }

    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        UsernamePasswordToken token = (UsernamePasswordToken)authenticationToken;
        String username = token.getPrincipal().toString();
        SysUser user = userService.getUserByUsername(username);
        if(user == null){
            throw new AuthenticationException();
        }
        String passwordInDB = user.getPassword();
        String salt = user.getSalt();
        //下面这一步做的是密码验证
        //下面该类存在多种构造方法
        SimpleAuthenticationInfo a = new SimpleAuthenticationInfo(user, passwordInDB, ByteSource.Util.bytes(salt),
                getName());
        return a;
    }
}
